package pl.javarsovia.javarsovia4android.adapters;

import static pl.javarsovia.javarsovia4android.util.UIUtils.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pl.javarsovia.R;
import pl.javarsovia.javarsovia4android.domain.Lecture;
import pl.javarsovia.javarsovia4android.domain.Slot;
import pl.javarsovia.javarsovia4android.domain.Speaker;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;

public class MySessionAdapter extends BaseAdapter implements ListAdapter {

	private final List<Slot> slots;
	private final Context ctx;
	private Map<Integer, Integer> positionToRoom = new HashMap<Integer, Integer>();

	public MySessionAdapter(Context ctx, List<Slot> slots) {
		this.ctx = ctx;
		this.slots = slots;
		for (int i = 0; i < slots.size(); i++) {
			positionToRoom.put(i, 0);
		}
	}

	@Override
	public int getCount() {
		return slots.size();
	}

	@Override
	public Slot getItem(int position) {
		return slots.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		view = getView(view);
		View nextLecture = view.findViewById(R.id.next_lecture);
		nextLecture.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				nextFor(position);
			}

		});
		View prevLecture = view.findViewById(R.id.prev_lecture);
		prevLecture.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				prevFor(position);
			}

		});
		Lecture lecture = getLectureFrom(position);
		fillTextView(view, R.id.hours, lecture.getSlot().getStart() + " - " + lecture.getSlot().getFinish());
		fillTextView(view, R.id.lecture_name, lecture.getTitle());
		fillTextView(view, R.id.room, lecture.getRoom().getName());
		setSpeakerInfo(view, position);
		// view.setMinimumHeight(90);
		// view.setClickable(true);
		return view;
	}

	private void setSpeakerInfo(View view, int position) {
		Speaker speaker = getLectureFrom(position).getSpeaker();
		fillTextView(view, R.id.speaker_name, getSpeakerName(speaker));
		view.setClickable(speaker == null);
	}

	private String getSpeakerName(Speaker speaker) {
		return speaker == null ? "" : speaker.getName();
	}

	public Lecture getLectureFrom(int position) {
		List<Lecture> lectures = getItem(position).getLectures();
		return lectures.get(positionToRoom.get(position));
	}

	private View getView(View view) {
		if (view == null) {
			LayoutInflater li = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = li.inflate(R.layout.my_session_row, null);
		}
		return view;
	}

	private void nextFor(int position) {
		moveRoomIn(position, 1);
	}

	private void prevFor(int position) {
		moveRoomIn(position, -1);
	}

	private void moveRoomIn(int position, int dir) {
		Integer room = positionToRoom.get(position);
		int roomIdx = room + dir;
		System.out.println("before changing " + roomIdx);
		if (roomIdx >= 0 && roomIdx < slots.get(position).getLectures().size()) {
			System.out.println("changing " + roomIdx);
			positionToRoom.put(position, roomIdx);
			notifyDataSetChanged();
		}
	}
}
